package com.ydd.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ydd.context.BaseContext;
import com.ydd.dto.GetSystemConfigDto;
import com.ydd.dto.IndexLoginDTO;
import com.ydd.dto.LoginDTO;
import com.ydd.dto.LoginDTO;
import com.ydd.entity.Page.AllCate;
import com.ydd.entity.Page.HotAtricle;
import com.ydd.entity.Page.NewUserMessage;
import com.ydd.entity.Page.UserMessage;
import com.ydd.entity.SysDict;
import com.ydd.entity.SysMenu;
import com.ydd.entity.SysUser;
import com.ydd.entity.YddUser;
import com.ydd.entity.profile.DictInfo;
import com.ydd.entity.profile.UserInfo;
import com.ydd.exception.AccountNotFoundException;
import com.ydd.exception.PasswordErrorException;
import com.ydd.mapper.*;
import com.ydd.service.indexSever;
import com.ydd.vo.BaseInfoVo;
import com.ydd.vo.GetConfigVO;

import com.ydd.vo.GetProfileVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author Shadow
 * @Date 2023-04-28 11:47
 */
@Service
public class indexSeverImp implements indexSever {
    @Autowired
    //管理员数据访问
    private SysUserMapper sysUserMapper;
    @Autowired
    //页面设置参数数据访问
    private SysConfigMapper sysConfigMapper;
    @Autowired
    //广告参数数据访问
    private YddAdvposMapper yddAdvposMapper;
    @Autowired
    //标签参数数据访问
    private YddTagMapper yddTagMapper;
    @Autowired
    //文章参数数据访问
    private YddArticleMapper yddArticleMapper;
    @Autowired
    //分类总数参数数据访问
    private YddCateMapper yddCateMapper;
    @Autowired
    //广告图片参数数据访问
    private YddAdvimgMapper yddAdvimgMapper;
    @Autowired
    //用户参数数据访问
    private YddUserMapper yddUserMapper;
    @Autowired
    //角色表参数数据访问
    private SysRoleMapper sysRoleMapper;
    @Autowired
    //菜单表参数数据访问
    private SysMenuMapper sysMenuMapper;
    @Autowired
    //角色菜单表数据访问
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    //字典表参数数据访问
    private SysDictMapper sysDictMapper;

    @Value("itheima")
    private String salt;

    @Override
    public SysUser ComparePassword(LoginDTO loginDto) {
        String loginName = loginDto.getLogin_name();
        String password = loginDto.getPassword();
        System.out.println("ssssssssssssssssssssssssss1");
        System.out.println(loginName);
        //获得用户对象
        SysUser sysUser= sysUserMapper.getPassWord(loginName);
        System.out.println("ssssssssssssssssssssssssss2");
        System.out.println(sysUser);

       //判用户对象是否为空
       if (sysUser==null)
       {
           throw new AccountNotFoundException("账号不存在异常");
       }
        //若不为空，校验加盐密码
        password = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        //若密码不正确
       if (!sysUser.getPassword().equals(password))
       {
           throw new PasswordErrorException("密码不正确异常");
       }
       //若账号被冻结   0为冻结 1为可登录
        if (sysUser.getStatus()==0)
        {
            throw new PasswordErrorException("密码不正确异常");
        }
        return sysUser;
    }

    @Override
    public GetConfigVO getSystemConfig(GetSystemConfigDto getSystemConfigDto) {
        System.out.println("ssssssssssssssssssssssssssssssssssssss");
        System.out.println(getSystemConfigDto);

        GetConfigVO getConfigVO = new GetConfigVO();
        //暂时没想到能一次全获的方法 已经尝试通过用In方法进行限制，获得的都是LIst 需要将列项目无法转换为横项目变成结构体
        getConfigVO.setSystem_logo(sysUserMapper.getSystemConfig(getSystemConfigDto.getSystem_logo()));
        getConfigVO.setLogin_title(sysUserMapper.getSystemConfig(getSystemConfigDto.getLoginTitle()));
        getConfigVO.setLogin_bg(sysUserMapper.getSystemConfig(getSystemConfigDto.getLogin_bg()));
        getConfigVO.setWeb_ico(sysUserMapper.getSystemConfig(getSystemConfigDto.getWeb_ico()));
        getConfigVO.setSystem_name(sysUserMapper.getSystemConfig(getSystemConfigDto.getSystem_name()));

        System.out.println("ssssssssssssssssssssssssssssssssssssss");
        System.out.println(getConfigVO);

        return getConfigVO;
    }

    @Override
    public BaseInfoVo getBaseinfo() {
        BaseInfoVo baseInfoVo=new BaseInfoVo();
        //第一部分：封装对象数据
        //用户信息男女统计
        //平台注册用户总数
        Integer TotalUser=yddUserMapper.gettotalNumber();
        //平台注册男用户总数
        Integer TotalManUser=yddUserMapper.getMantotalNumber();
        //平台注册女用户总数
        Integer TotalLadyUser=TotalUser-TotalManUser;
        //分装
        baseInfoVo.setUserMessage(UserMessage.builder()
                .total(TotalUser)
                .user_man(TotalManUser)
                .user_woman(TotalLadyUser).build());
        //第二部分
        //获得各分类文章发布数量链表
        List<AllCate> allCates=yddCateMapper.getCatesList();
        baseInfoVo.setAllCate(allCates);
        //点赞文章排行列表
        List<HotAtricle> hotAtricles=yddArticleMapper.get7HotAtricle();
        baseInfoVo.setHotAtricle(hotAtricles);
        //最新注册用户列表
        List<NewUserMessage> newUserList=yddUserMapper.get7userList();
        baseInfoVo.setNewUserMessage(newUserList);
        //第二部分：单对点的值
        //管理员用户总数
        baseInfoVo.setAdmin_num(sysUserMapper.gettotalNumber());
        //平台注册用户总数
        //平台文章总数
        baseInfoVo.setArticle_num(yddArticleMapper.gettotalNumber());
        //广告位总数
        baseInfoVo.setAdvpos_num(yddAdvposMapper.gettotalNumber());

        return baseInfoVo;
    }

    @Override
    public YddUser login(IndexLoginDTO indexLoginDTO) {
        String mobile = indexLoginDTO.getMobile();
        String password = indexLoginDTO.getPassword();

        password = DigestUtils.md5DigestAsHex((indexLoginDTO.getPassword() + salt).getBytes());

        YddUser yddUser = yddUserMapper.login(mobile);
        if (yddUser == null || !yddUser.getPassword().equals(password)) {
            return null;
        }

        yddUser.setPassword(password);
        return yddUser;
    }

    @Override
    public GetProfileVo GetProfile() {
        GetProfileVo getProfileVo=new GetProfileVo();
        SysUser sysUserById = sysUserMapper.getSysUserById(BaseContext.getCurrentId());
        String RoleIds = sysUserById.getRoleId();

        //处理Roleid数组
        List<String> getRolesStringId=new ArrayList<>();
        String[] rolesId=RoleIds.split("\"");
        for (int i = 1; i < rolesId.length; i+=2) {
            getRolesStringId.add(rolesId[i]);
        }
        //不联表  空指针会报错
        //里面是数组需要剪切
        List<String> RoleName=new ArrayList<>();
        for (String s : getRolesStringId) {
            RoleName.add(sysRoleMapper.getRoleNameById(s));
        }
        //封装UserInfo
        UserInfo userInfo=new UserInfo();
        BeanUtils.copyProperties(sysUserById,userInfo);//左实，右空
        userInfo.setRoleId(getRolesStringId);
        userInfo.setRolename(RoleName);

        getProfileVo.setUserInfo(userInfo);
        //通过角色菜单Id联动找到所属菜单Id  list添加list
        List<Integer> Menusids=new ArrayList<>();
        for (String s : getRolesStringId) {
            Menusids.addAll(sysRoleMenuMapper.findMenuIdListByroleId(s));
        }
        List<SysMenu> sysMenu=sysMenuMapper.getSysMenuByIds(Menusids);
        getProfileVo.setMenuList(sysMenu);

        //获得字典相应的值列表
       //LambdaQueryWrapper<SysDict> DictWrapper=new LambdaQueryWrapper<>();
       //DictWrapper.select(SysDict::getValue,SysDict::getType,SysDict::getLabel);
       //List<SysDict> sysDicts=sysDictMapper.selectList(DictWrapper);

        List<SysDict> sysDicts=sysDictMapper.getsysDictsList();

        //封装字典
        Map<Integer,String> table_type=new HashMap<>();
        Map<String,String> interface_type=new HashMap<>();
        Map<Integer,String> report_type=new HashMap<>();
        Map<Integer,String> order_type=new HashMap<>();
        Map<String,String>  theme =new HashMap<>();
        for (SysDict sysDict : sysDicts) {
            switch (sysDict.getType())
            {
                case "order_type":
                    order_type.put(Integer.valueOf(sysDict.getValue()),sysDict.getLabel());
                    break;
                case "interface_type":
                    interface_type.put(sysDict.getValue(),sysDict.getLabel());
                    break;
                case "report_type":
                    report_type.put(Integer.valueOf(sysDict.getValue()),sysDict.getLabel());
                    break;
                case "table_type":
                    table_type.put(Integer.valueOf(sysDict.getValue()),sysDict.getLabel());
                    break;
                case "theme":
                    theme.put(sysDict.getValue(),sysDict.getLabel());
                    break;
                default:
                    break;
            }
        }

        //封装dictInfo
        DictInfo dictInfo=new DictInfo();
        dictInfo.setInterface_type(interface_type);
        dictInfo.setTable_type(table_type);
        dictInfo.setReport_type(report_type);
        dictInfo.setOrder_type(order_type);
        dictInfo.setTheme(theme);
        getProfileVo.setDictsList(dictInfo);

        //binList
        List<String> binList=new ArrayList<>();
        binList.add("*");
        getProfileVo.setBinList(binList);


        return getProfileVo;
    }


}

